﻿@page "/"
@rendermode InteractiveWebAssembly
@inject IRepository PizzaStore


<PageTitle>Blazing Pizzas</PageTitle>

<div class="main">
	<ul class="pizza-cards">
		@if (specials is not null)
		{
			@foreach (var special in specials)
			{
				<li @onclick="@(()=>ShowConfigurePizzaDialog(special))" style="background-image: url('@special.ImageUrl')">
					<div class="pizza-info">
						<span class="title">@special.Name</span>
						@special.Description
						<span class="price">@special.GetFormattedBasePrice()</span>
					</div>
				</li>
			}
		}
	</ul>
</div>

<div class="sidebar">
	@if (order.Pizzas.Any())
	{
		<div class="order-contents">
			<h2>Your order</h2>

			@foreach (var configuredPizza in order.Pizzas)
			{
				<ConfiguredPizzaItem Pizza="configuredPizza" OnRemoved="@(() => RemoveConfiguredPizza(configuredPizza))" />
			}
		</div>
	}
	else
	{
		<div class="empty-cart">Choose a pizza<br>to get started</div>
	}

	<div class="order-total @(order.Pizzas.Any() ? "" : "hidden")">
		Total:
		<span class="total-price">@order.GetFormattedTotalPrice()</span>
		<button class="btn btn-warning" disabled="@(order.Pizzas.Count == 0)" @onclick="PlaceOrder">
			Order >
		</button>
	</div>
</div>

@if (showingConfigureDialog)
{
	<ConfigurePizzaDialog Pizza="configuringPizza"
												OnCancel="CancelConfigurePizzaDialog"
												OnConfirm="ConfirmConfigurePizzaDialog" />
}

@code {
	List<PizzaSpecial>? specials;
	Pizza? configuringPizza;
	bool showingConfigureDialog;
	Order order = new Order();

	protected override async Task OnInitializedAsync()
	{
		specials = await PizzaStore.GetSpecials();
	}

	void ShowConfigurePizzaDialog(PizzaSpecial special)
	{
		configuringPizza = new Pizza()
			{
				Special = special,
				SpecialId = special.Id,
				Size = Pizza.DefaultSize,
				Toppings = new List<PizzaTopping>(),
			};

		showingConfigureDialog = true;
	}

	void CancelConfigurePizzaDialog()
	{
		configuringPizza = null;
		showingConfigureDialog = false;
	}

	void ConfirmConfigurePizzaDialog()
	{
		if (configuringPizza is not null)
		{
			order.Pizzas.Add(configuringPizza);
			configuringPizza = null;
		}

		showingConfigureDialog = false;
	}

	void RemoveConfiguredPizza(Pizza pizza)
	{
		order.Pizzas.Remove(pizza);
	}

	async Task PlaceOrder()
	{
		await PizzaStore.PlaceOrder(order);
		order = new Order();
	}

}